#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
	list<int> l1 = { 3,4,5,2,1 };

	int x;
	while (cin >> x)
	{
		auto pos = find(l1.begin(), l1.end(), x);
		l1.splice(l1.begin(), l1, pos);
		for (auto e : l1)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	return 0;
}

int main3()
{
	list<int> l1 = { 1,2,2,4 };
	list<int> l2 = { 9,6,6,5,8 };
	l1.sort();
	l2.sort();

	l1.merge(l2);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;

	l1.unique();
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	l1.remove(4);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	return 0;
}

int main2()
{
	list<int> l1 = { 9,6,3,5,4,7,122,54,8,6,5,1,56,4,0 };
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	l1.sort();
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	return 0;
}

int main1()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	l1.emplace_back(4);
	l1.emplace_back(4);
	l1.emplace_back(4);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	int x;
	cin >> x;
	list<int>::iterator it = find(l1.begin(), l1.end(), x);
	if (it != l1.end())
	{
		l1.erase(it);
	}
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	l1.reverse();
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	return 0;
}